#include <Arduino.h>
#include <Wire.h>
#include <WiFi.h>
#include <DHT.h>
#include <HttpClient.h>

const char *ssid = "fei";//WIFI
const char *password = "18832825018yf";//WIFI密码
String url = "https://www.feishu.cn/flow/api/trigger-webhook/2b3565ba9d437ba7035fd239d6aa2ea7";//网址 
WiFiClient client; //声明一个客户端对象，用于与服务器进行连接


#define LED_BUILTIN 2
#define KEY         13
#define DHTPIN      27 //DHT11 DATA 数据引脚

#define DHTTYPE       DHT11 //选择的类型
DHT dht(DHTPIN, DHTTYPE);

float lab_h = 0, lab_t = 0;

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 8*3600;//这里采用UTC计时，中国为东八区，就是 8*60*60
const int   daylightOffset_sec = 0;//同上

int chipid; 

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

void setup(void)
{
    uint64_t chipid_tem = ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
    chipid = chipid_tem;
    pinMode(LED_BUILTIN, OUTPUT);
    pinMode(KEY, INPUT);
    pinMode(KEY, INPUT_PULLUP);

    Serial.begin(115200);
    Serial.println("Hello World!");

    //DHT11
    dht.begin();
    
    WiFi.begin(ssid, password);//连接到网络

   while (WiFi.status() != WL_CONNECTED) {//等待网络连接成功
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());//打印模块IP

    //init and get the time
    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    printLocalTime();

}

void loop(void)
{
    int key = digitalRead(KEY);

    //DHT11
    lab_h = dht.readHumidity();
    lab_t = dht.readTemperature();
    Serial.printf("dht11: %.2f, %.2f ",lab_t, lab_h);
    if (isnan(lab_h) || isnan(lab_t))
    {
         Serial.println("Failed to read from DHT sensor!");
         return;
    }
    printLocalTime();
    struct tm timeinfo;
    if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    }

    Serial.printf("%4d-%02d-%02d  %02d:%02d:%02d",
                    1900+timeinfo.tm_year,
                    1+timeinfo.tm_mon,
                    timeinfo.tm_mday,
                    timeinfo.tm_hour,
                    timeinfo.tm_min,
                    timeinfo.tm_sec);

    HTTPClient http;
    http.begin(url);
    http.addHeader("Content-Type", "application/json");

     String postMessage="{\"data\": {\"deviceID\": " + String(chipid) + ",\"time\": \"" + String(1900+timeinfo.tm_year) + "-"  + String(1+timeinfo.tm_mon) 
                        + "-"  + String(timeinfo.tm_mday) 
                        + " "  + String(timeinfo.tm_hour) 
                        + ":"  + String(1+timeinfo.tm_min) 
                        + ":"  + String(1+timeinfo.tm_sec) 
                        + "\",\"temp\": " +  String(lab_t) + ",\"hump\": " + String(lab_h) + "}}";
    Serial.print(postMessage);
    int httpCode = http.POST(postMessage);
    Serial.printf("httpCode: %d ",httpCode);
    // if(key)
    //     digitalWrite(LED_BUILTIN, LOW);
    // else digitalWrite(LED_BUILTIN, HIGH);

    
    delay(1800000);
    // digitalWrite(LED_BUILTIN, LOW);
    // delay(360000);
    // digitalWrite(LED_BUILTIN, HIGH);
}

